package cn.edu.cqvie.netty.http;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * HTTP 服务器，实现通过浏览器访问8899端口返回 Hello World
 *
 * @author zhengsh
 */
public class TestServer {

    public static void main(String[] args) {

        //1、接受连接
        EventLoopGroup bossGrop = new NioEventLoopGroup();
        //2、处理连接
        EventLoopGroup workerGrop = new NioEventLoopGroup();

        try {
            //3、启动服务
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap
                    //设置服务组
                    .group(bossGrop, workerGrop)
                    //管道【反射】
                    .channel(NioServerSocketChannel.class)
                    //定义子处理器
                    .childHandler(new TestServerInitializer());
            //4、绑定端口
            ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }  finally {
            //5、优雅关闭
            bossGrop.shutdownGracefully();
            workerGrop.shutdownGracefully();
        }
    }
}
